using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Jurassic;
using Jurassic.Library;

namespace Kiwi.Node.Utility
{
    public static class FormatHelper
    {
        private static readonly Regex MatchFormat = new Regex(@"\%[\%sdj]");
        public static string Format(ScriptEngine engine, string format, params object[] arguments)
        {
            if (arguments.Length == 0)
            {
                return format;
            }

            var q = new Queue<object>(arguments);
            var formatted =  MatchFormat.Replace(format ?? "", m =>
                                                                   {
                                                                       if (q.Count == 0)
                                                                       {
                                                                           return m.Value;
                                                                       }
                                                                       var arg = q.Dequeue();
                                                                       switch (m.Value[1])
                                                                       {
                                                                           case '%':
                                                                               return "%%";
                                                                           case 'd':
                                                                               return arg is Null ? "" : TypeConverter.ToPrimitive(arg, PrimitiveTypeHint.Number).ToString();
                                                                           case 's':
                                                                               return arg is Null ? "" : arg.ToString();
                                                                           case 'j':
                                                                               return JSONObject.Stringify(engine, arg);
                                                                       }
                                                                       return m.Value;
                                                                   });

            if (q.Count > 0)
            {
                var sb = new StringBuilder();
                sb.Append(formatted);
                while (q.Count > 0)
                {
                    sb.Append(' ');
                    sb.Append(Inspect(engine, q.Dequeue()));
                }
                formatted = sb.ToString();
            }
            return formatted;
        }

        public static string Inspect(ScriptEngine engine, object @object, bool showHidden = false, int depth = int.MaxValue, bool colors = false)
        {
            if ((@object is FunctionInstance) || (@object is Undefined) || (@object is Null))
            {
                return @object.ToString();
            }

            return JSONObject.Stringify(engine, @object);
        }

    }
}